package com.glorysoft.sunnypda.utils;

import android.annotation.SuppressLint;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;

import com.glorysoft.sunnypda.ui.reportrepair.ReportRepairActivity;

import java.lang.reflect.Method;

public class NotificationClickReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        collapsingNotification(context);
        expandNotification(context);
        Intent newIntent = new Intent(context, ReportRepairActivity.class).addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        intent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
        context.startActivity(newIntent);
    }
    //折叠通知栏
    public static void collapsingNotification(Context context) {
        Object service = context.getSystemService("statusbar");
        if (null == service){
            return;
        }
        try {

            Class clazz = Class.forName("android.app.StatusBarManager");

            int sdkVersion = android.os.Build.VERSION.SDK_INT;

            Method collapse = null;

            if (sdkVersion <= 16) {
                collapse = clazz.getMethod("collapse");
            } else {
                collapse = clazz.getMethod("collapsePanels");
            }
            collapse.setAccessible(true);
            collapse.invoke(service);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    //展开通知栏
    public static void expandNotification(Context context) {
        Object service = context.getSystemService("statusbar");
        if (null == service) {
            return;
        }
        try {
            Class clazz = Class.forName("android.app.StatusBarManager");
            int sdkVersion = android.os.Build.VERSION.SDK_INT;
            Method expand = null;
            if (sdkVersion <= 16) {
                expand = clazz.getMethod("expand");
            } else {
                expand = clazz.getMethod("expandSettingsPanel");
            }
            expand.setAccessible(true);
            expand.invoke(service);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
